using System;
using System.Reflection;
using System.Windows.Input;
namespace System.Windows.Interactivity
{
	public sealed class InvokeCommandAction : TriggerAction<DependencyObject>
	{
		private string commandName;
		public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(InvokeCommandAction), null);
		public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(InvokeCommandAction), null);
		public string CommandName
		{
			get
			{
				return this.commandName;
			}
			set
			{
				if (this.CommandName != value)
				{
					this.commandName = value;
				}
			}
		}
		public ICommand Command
		{
			get
			{
				return (ICommand)base.GetValue(InvokeCommandAction.CommandProperty);
			}
			set
			{
				base.SetValue(InvokeCommandAction.CommandProperty, value);
			}
		}
		public object CommandParameter
		{
			get
			{
				return base.GetValue(InvokeCommandAction.CommandParameterProperty);
			}
			set
			{
				base.SetValue(InvokeCommandAction.CommandParameterProperty, value);
			}
		}
		protected override void Invoke(object parameter)
		{
			if (base.AssociatedObject != null)
			{
				ICommand command = this.ResolveCommand();
				if (command != null && command.CanExecute(this.CommandParameter))
				{
					command.Execute(this.CommandParameter);
				}
			}
		}
		private ICommand ResolveCommand()
		{
			ICommand result = null;
			if (this.Command != null)
			{
				result = this.Command;
			}
			else
			{
				if (base.AssociatedObject != null)
				{
					Type type = base.AssociatedObject.GetType();
					PropertyInfo[] properties = type.GetProperties(20);
					PropertyInfo[] array = properties;
					for (int i = 0; i < array.Length; i++)
					{
						PropertyInfo propertyInfo = array[i];
						if (typeof(ICommand).IsAssignableFrom(propertyInfo.get_PropertyType()) && string.Equals(propertyInfo.get_Name(), this.CommandName, 4))
						{
							result = (ICommand)propertyInfo.GetValue(base.AssociatedObject, null);
						}
					}
				}
			}
			return result;
		}
	}
}
